package com.pai4j.common.util;

/**
 * @Author: 程序员七七，程序员盒子（https://www.coderutil.com）作者
 * @Date: 2021-5-22 0022 21:42
 * @Description: 无描述信息
 */

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.Executor;

/***
 * 并行处理器
 */
@Slf4j
public class BulkHandleExecutor {

    private CountDownLatch latch;
    private Executor executor;

    public BulkHandleExecutor(CountDownLatch latch, Executor executor) {
        this.latch = latch;
        this.executor = executor;
    }

    public void execute(Runnable runnable) {
        executor.execute(() -> {
            try {
                runnable.run();
            } catch (Exception e) {
                log.error("BulkHandleExecutor.execute error", e);
            } finally {
                latch.countDown();
            }
        });
    }

    public void await() {
        try {
            latch.await();
        } catch (Exception e) {
            log.error("BulkHandleExecutor.await error", e);
        }
    }
}
